package com.sphd.restaurantsystem.activitys.push;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sphd.restaurantsystem.R;
import com.sphd.restaurantsystem.base.BaseActivity;
import com.sphd.restaurantsystem.base.RestaurantUrl;
import com.sphd.restaurantsystem.info.LoginInfo;
import com.sphd.restaurantsystem.info.PushMoneyInfo;
import com.sphd.restaurantsystem.info.PushScaleInfo;
import com.sphd.restaurantsystem.info.RestaurantInfo;
import com.sphd.restaurantsystem.presenter.PushMoneyPresenterImpl;
import com.sphd.restaurantsystem.utils.Constant;
import com.sphd.restaurantsystem.view.PushMoneyView;

import org.litepal.crud.DataSupport;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class PushMoneyActivity extends BaseActivity implements PushMoneyView {

    @BindView(R.id.menu_push_set)
    TextView menuPushSet;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.push_yesterday)
    TextView pushYesterday;
    @BindView(R.id.first_lift)
    TextView firstLift;
    @BindView(R.id.today_first_lift)
    TextView todayFirstLift;
    @BindView(R.id.first_centre)
    TextView firstCentre;
    @BindView(R.id.today_first_centre)
    TextView todayFirstCentre;
    @BindView(R.id.first_right)
    TextView firstRight;
    @BindView(R.id.today_first_right)
    TextView todayFirstRight;
    @BindView(R.id.second_lift)
    TextView secondLift;
    @BindView(R.id.today_second_lift)
    TextView todaySecondLift;
    @BindView(R.id.second_centre)
    TextView secondCentre;
    @BindView(R.id.today_second_centre)
    TextView todaySecondCentre;
    @BindView(R.id.second_right)
    TextView secondRight;
    @BindView(R.id.today_second_right)
    TextView todaySecondRight;
    @BindView(R.id.today_most_lift)
    TextView todayMostLift;
    @BindView(R.id.today_lift)
    TextView todayLift;
    @BindView(R.id.today_most_centre)
    TextView todayMostCentre;
    @BindView(R.id.today_centre)
    TextView todayCentre;
    @BindView(R.id.today_most_right)
    TextView todayMostRight;
    @BindView(R.id.today_right)
    TextView todayRight;
    @BindView(R.id.tv_this_month)
    TextView tvThisMonth;
    @BindView(R.id.tv_last_month)
    TextView tvLastMonth;
    @BindView(R.id.title_month)
    TextView titleMonth;
    @BindView(R.id.sum_month)
    TextView sumMonth;
    @BindView(R.id.month_most_lift)
    TextView monthMostLift;
    @BindView(R.id.month_lift)
    TextView monthLift;
    @BindView(R.id.month_most_centre)
    TextView monthMostCentre;
    @BindView(R.id.month_centre)
    TextView monthCentre;
    @BindView(R.id.month_most_right)
    TextView monthMostRight;
    @BindView(R.id.month_right)
    TextView monthRight;
    @BindView(R.id.content_push_money)
    LinearLayout contentPushMoney;
    Intent intent;
    Bundle bundle;
    RestaurantInfo restaurantInfo;
    LoginInfo loginInfo;
    PushMoneyPresenterImpl pushMoneyPresenter;
    int setListSize = 0;
    List<PushMoneyInfo> pushMoneyInfos;
    List<PushScaleInfo> scaleInfos;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_push_money);
        addActivity(this);
        ButterKnife.bind(this);
        initView();
        initEvent();
    }

    private void initEvent() {
        toolbar.setNavigationOnClickListener(view -> finishActivity(this));
    }

    private void initView() {
        intent = getIntent();
        bundle = intent.getExtras();
        setTitleBar(toolbar, "提成");
        restaurantInfo = (RestaurantInfo) bundle.getSerializable(Constant.RESTAURANTSINFO);
        loginInfo = DataSupport.findLast(LoginInfo.class);
        pushMoneyPresenter = new PushMoneyPresenterImpl(this);
        if (!isSuperManager(loginInfo)) {
            menuPushSet.setVisibility(View.GONE);
        }
        addData();
    }

    private void addData() {
        pushMoneyPresenter.addData(RestaurantUrl.Httptitle + RestaurantUrl.PUSHHOME + "cgid=" + restaurantInfo.getId());
    }

    @Override
    public void recRes(List<PushMoneyInfo> pushMoneyInfos, List<PushScaleInfo> scaleInfos, double daytopup, double dayorder0, double dayorder1) {


        if (scaleInfos.size() == 0) {
            if (isSuperManager(loginInfo)) {
                bundle.putInt(Constant.TYPE, 1);
                intent.putExtras(bundle);
                intent.setClass(this, SetPushMoneyActivity.class);
                startActivityForResult(intent, 1);
            }
            return;
        } else {
            setUi(scaleInfos, pushMoneyInfos, daytopup, dayorder0, dayorder1);
        }
    }


    /**
     * @param scaleInfos     三种提成比例
     * @param pushMoneyInfos 包含 今天的金额 本月的金额  上月的金额
     * @param daytopup       充值金额
     * @param dayorder0      一般消费金额
     * @param dayorder1      打折卡消费金额
     */
    private void setUi(List<PushScaleInfo> scaleInfos, List<PushMoneyInfo> pushMoneyInfos, double daytopup, double dayorder0, double dayorder1) {


        this.scaleInfos = scaleInfos;
        this.pushMoneyInfos = pushMoneyInfos;
        setNULL();
        setListSize = scaleInfos.size();
        pushYesterday.setText(turn(pushMoneyInfos.get(0).getSalary_sum(), Constant.DECIMAL_POINT_TWO) + "");
        if (setListSize == 1) {
            for (PushScaleInfo pushScaleInfo : scaleInfos) {
                if (pushScaleInfo.getSalary_type() == 2) {
                    setTextType(1, "充值金额提成", pushMoneyInfos.get(0).getSalary_topup(), "充值金额", daytopup,
                            "充值提成比例", pushScaleInfo.getSalary_percent());
                } else if (pushScaleInfo.getSalary_type() == 1) {
                    setTextType(1, "打折卡消费提成", pushMoneyInfos.get(0).getSalary_discount(), "打折卡消费金额", dayorder1,
                            "打折卡提成比例", pushScaleInfo.getSalary_percent());
                } else if (pushScaleInfo.getSalary_type() == 0) {
                    setTextType(1, "一般消费提成", pushMoneyInfos.get(0).getSalary_common(),
                            "一般消费金额", dayorder0, "一般消费提成比例", pushScaleInfo.getSalary_percent());
                }
            }

        } else if (setListSize == 2) {
            if (scaleInfos.get(0).getSalary_type() == 2) {
                setTextType(0, "充值金额提成", pushMoneyInfos.get(0).getSalary_topup(), "充值金额", daytopup, "充值提成比例",
                        scaleInfos.get(0).getSalary_percent());

            } else if (scaleInfos.get(0).getSalary_type() == 1) {
                setTextType(0, "打折卡消费提成", pushMoneyInfos.get(0).getSalary_discount(), "打折卡消费金额", dayorder1,
                        "打折卡提成比例", scaleInfos.get(0).getSalary_percent());

            } else if (scaleInfos.get(0).getSalary_type() == 0) {
                setTextType(0, "一般消费提成", pushMoneyInfos.get(0).getSalary_common(),
                        "一般消费金额", dayorder1, "一般消费提成比例", scaleInfos.get(0).getSalary_percent());
            }

            if (scaleInfos.get(1).getSalary_type() == 2) {
                setTextType(2, "充值金额提成", pushMoneyInfos.get(0).getSalary_topup(), "充值金额", daytopup, "充值提成比例",
                        scaleInfos.get(1).getSalary_percent());

            } else if (scaleInfos.get(1).getSalary_type() == 1) {
                setTextType(2, "打折卡消费提成", pushMoneyInfos.get(0).getSalary_discount(), "打折卡消费金额", dayorder1,
                        "打折卡提成比例", scaleInfos.get(1).getSalary_percent());

            } else if (scaleInfos.get(1).getSalary_type() == 0) {
                setTextType(2, "一般消费提成", pushMoneyInfos.get(0).getSalary_common(),
                        "一般消费金额", dayorder0, "一般消费提成比例", scaleInfos.get(1).getSalary_percent());
            }
        } else if (setListSize == 3) {
            if (scaleInfos.get(0).getSalary_type() == 2) {
                setTextType(0, "充值金额提成", pushMoneyInfos.get(0).getSalary_topup(), "充值金额", daytopup, "充值提成比例",
                        scaleInfos.get(0).getSalary_percent());

            } else if (scaleInfos.get(0).getSalary_type() == 1) {
                setTextType(0, "打折卡消费提成", pushMoneyInfos.get(0).getSalary_discount(), "打折卡消费金额", dayorder1,
                        "打折卡提成比例", scaleInfos.get(0).getSalary_percent());

            } else if (scaleInfos.get(0).getSalary_type() == 0) {
                setTextType(0, "一般消费提成", pushMoneyInfos.get(0).getSalary_common(),
                        "一般消费金额", dayorder0, "一般消费提成比例", scaleInfos.get(0).getSalary_percent());
            }

            if (scaleInfos.get(1).getSalary_type() == 2) {
                setTextType(1, "充值金额提成", pushMoneyInfos.get(0).getSalary_topup(), "充值金额", daytopup, "充值提成比例",
                        scaleInfos.get(1).getSalary_percent());

            } else if (scaleInfos.get(1).getSalary_type() == 1) {
                setTextType(1, "打折卡消费提成", pushMoneyInfos.get(0).getSalary_discount(), "打折卡消费金额", dayorder1,
                        "打折卡提成比例", scaleInfos.get(1).getSalary_percent());

            } else if (scaleInfos.get(1).getSalary_type() == 0) {
                setTextType(1, "一般消费提成", pushMoneyInfos.get(0).getSalary_common(),
                        "一般消费金额", dayorder0, "一般消费提成比例", scaleInfos.get(1).getSalary_percent());
            }

            if (scaleInfos.get(2).getSalary_type() == 2) {
                setTextType(2, "充值金额提成", pushMoneyInfos.get(0).getSalary_topup(), "充值金额", daytopup, "充值提成比例",
                        scaleInfos.get(2).getSalary_percent());

            } else if (scaleInfos.get(2).getSalary_type() == 1) {
                setTextType(2, "打折卡消费提成", pushMoneyInfos.get(0).getSalary_discount(), "打折卡消费金额", dayorder1,
                        "打折卡提成比例", scaleInfos.get(2).getSalary_percent());

            } else if (scaleInfos.get(2).getSalary_type() == 0) {
                setTextType(2, "一般消费提成", pushMoneyInfos.get(0).getSalary_common(),
                        "一般消费金额", dayorder0, "一般消费提成比例", scaleInfos.get(2).getSalary_percent());
            }

        }

        setMonthUi(1, "本月提成总额（元）");
    }

    private void setTextType(int i, String firtTitle, double salary_topup, String secondTiele, double daytopup, String thirdTitle, double rech) {
        if (i == 0) {
            todayMostLift.setText(firtTitle);
            todayLift.setText(turn(salary_topup, Constant.DECIMAL_POINT_TWO) + "");
            firstLift.setText(secondTiele);
            todayFirstLift.setText(turn(daytopup, Constant.DECIMAL_POINT_TWO) + "");
            secondLift.setText(thirdTitle);
            todaySecondLift.setText(rech + "%");
        } else if (i == 1) {
            todayMostCentre.setText(firtTitle);
            todayCentre.setText(turn(salary_topup, Constant.DECIMAL_POINT_TWO) + "");
            firstCentre.setText(secondTiele);
            todayFirstCentre.setText(turn(daytopup, Constant.DECIMAL_POINT_TWO) + "");
            secondCentre.setText(thirdTitle);
            todaySecondCentre.setText(rech + "%");
        } else if (i == 2) {
            todayMostRight.setText(firtTitle);
            todayRight.setText(turn(salary_topup, Constant.DECIMAL_POINT_TWO) + "");
            firstRight.setText(secondTiele);
            todayFirstRight.setText(turn(daytopup, Constant.DECIMAL_POINT_TWO) + "");
            secondRight.setText(thirdTitle);
            todaySecondRight.setText(rech + "%");
        }
    }

    private void setNULL() {
        setNull(firstLift);
        setNull(firstCentre);
        setNull(firstRight);
        setNull(todayFirstLift);
        setNull(todayFirstCentre);
        setNull(todayFirstRight);
        setNull(secondLift);
        setNull(secondCentre);
        setNull(secondRight);
        setNull(todaySecondLift);
        setNull(todaySecondCentre);
        setNull(todaySecondRight);
        setNull(todayMostLift);
        setNull(todayLift);
        setNull(todayMostCentre);
        setNull(todayCentre);
        setNull(todayMostRight);
        setNull(todayRight);
        setNull(monthMostLift);
        setNull(monthLift);
        setNull(monthMostCentre);
        setNull(monthCentre);
        setNull(monthMostRight);
        setNull(monthRight);
    }

    public void setNull(TextView aNull) {
        aNull.setText("");
    }

    @OnClick({R.id.menu_push_set, R.id.tv_this_month, R.id.tv_last_month})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.menu_push_set:
                // TODO: 2016/12/13
                bundle.putInt(Constant.TYPE, 0);
                intent.putExtras(bundle);
                intent.setClass(this, SetPushMoneyActivity.class);
                startActivityForResult(intent, 1);
                break;
            case R.id.tv_this_month:
                tvThisMonth.setTextColor(getResources().getColor(R.color.white));
                tvLastMonth.setTextColor(getResources().getColor(R.color.indigo_500));

                tvThisMonth.setBackgroundResource(R.mipmap.btn_blue);
                tvLastMonth.setBackgroundResource(R.mipmap.btn_white);

                setMonthUi(1, "本月提成总额（元）");
                break;
            case R.id.tv_last_month:
                tvThisMonth.setTextColor(getResources().getColor(R.color.indigo_500));
                tvLastMonth.setTextColor(getResources().getColor(R.color.white));

                tvThisMonth.setBackgroundResource(R.mipmap.btn_white);
                tvLastMonth.setBackgroundResource(R.mipmap.btn_blue);

                setMonthUi(2, "上月提成总额（元）");
                break;
        }
    }

    private void setMonthUi(int i, String s) {
        titleMonth.setText(s);
        sumMonth.setText(turn(pushMoneyInfos.get(i).getSalary_sum(), Constant.DECIMAL_POINT_TWO) + "");
        if (setListSize == 1) {
            if (scaleInfos.get(0).getSalary_type() == 2) {
                setMonthTextType(1, "充值金额提成", pushMoneyInfos.get(i).getSalary_topup());
            } else if (scaleInfos.get(0).getSalary_type() == 1) {
                setMonthTextType(1, "打折卡消费提成", pushMoneyInfos.get(i).getSalary_discount());
            } else if (scaleInfos.get(0).getSalary_type() == 0) {
                setMonthTextType(1, "一般消费提成", pushMoneyInfos.get(i).getSalary_common());
            }
        } else if (setListSize == 2) {
            if (scaleInfos.get(0).getSalary_type() == 2) {
                setMonthTextType(0, "充值金额提成", pushMoneyInfos.get(i).getSalary_topup());
            } else if (scaleInfos.get(0).getSalary_type() == 1) {
                setMonthTextType(0, "打折卡消费提成", pushMoneyInfos.get(i).getSalary_discount());
            } else if (scaleInfos.get(0).getSalary_type() == 0) {
                setMonthTextType(0, "一般消费提成", pushMoneyInfos.get(i).getSalary_common());
            }

            if (scaleInfos.get(1).getSalary_type() == 2) {
                setMonthTextType(2, "充值金额提成", pushMoneyInfos.get(i).getSalary_topup());

            } else if (scaleInfos.get(1).getSalary_type() == 1) {
                setMonthTextType(2, "打折卡消费提成", pushMoneyInfos.get(i).getSalary_discount());

            } else if (scaleInfos.get(1).getSalary_type() == 0) {
                setMonthTextType(2, "一般消费提成", pushMoneyInfos.get(i).getSalary_common());
            }
        } else if (setListSize == 3) {
            if (scaleInfos.get(0).getSalary_type() == 2) {
                setMonthTextType(0, "充值金额提成", pushMoneyInfos.get(i).getSalary_topup());
            } else if (scaleInfos.get(0).getSalary_type() == 1) {
                setMonthTextType(0, "打折卡消费提成", pushMoneyInfos.get(i).getSalary_discount());
            } else if (scaleInfos.get(0).getSalary_type() == 0) {
                setMonthTextType(0, "一般消费提成", pushMoneyInfos.get(i).getSalary_common());
            }

            if (scaleInfos.get(1).getSalary_type() == 2) {
                setMonthTextType(1, "充值金额提成", pushMoneyInfos.get(i).getSalary_topup());

            } else if (scaleInfos.get(1).getSalary_type() == 1) {
                setMonthTextType(1, "打折卡消费提成", pushMoneyInfos.get(i).getSalary_discount());

            } else if (scaleInfos.get(1).getSalary_type() == 0) {
                setMonthTextType(1, "一般消费提成", pushMoneyInfos.get(i).getSalary_common());
            }

            if (scaleInfos.get(2).getSalary_type() == 2) {
                setMonthTextType(2, "充值金额提成", pushMoneyInfos.get(i).getSalary_topup());

            } else if (scaleInfos.get(2).getSalary_type() == 1) {
                setMonthTextType(2, "打折卡消费提成", pushMoneyInfos.get(i).getSalary_discount());

            } else if (scaleInfos.get(2).getSalary_type() == 0) {
                setMonthTextType(2, "一般消费提成", pushMoneyInfos.get(i).getSalary_common());
            }

        }


    }

    private void setMonthTextType(int i, String title, double money) {
        if (i == 0) {
            monthMostLift.setText(title);
            monthLift.setText(turn(money, Constant.DECIMAL_POINT_TWO) + "");
        } else if (i == 1) {
            monthMostCentre.setText(title);
            monthCentre.setText(turn(money, Constant.DECIMAL_POINT_TWO) + "");
        } else if (i == 2) {
            monthMostRight.setText(title);
            monthRight.setText(turn(money, Constant.DECIMAL_POINT_TWO) + "");
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 0) {
            addData();
        }
    }

    @Override
    public void showLoading() {
        showLoadingDialog();
    }

    @Override
    public void dissLoading() {
        dissLoadingDialog();
    }
}
